package com.errol.generic.file.core.service;

import com.renjia.properties.UkerServerProperties;
import com.renjia.service.generic.base.service.InitServiceImpl;
import com.renjia.service.generic.file.core.pojo.Filepath;
import org.springframework.stereotype.Service;

import java.io.File;

/**
 * Created by Errol1017 on 2018/4/9.
 *
 * @author zhang xiao bin
 * @version v1.0
 * @date 2020/10/21
 * @since v4.7.6
 */
@Service
public class FileInitServiceImpl extends InitServiceImpl {

    private final String fileBasePath;
    private final FileInnerService fileInnerService;

    public FileInitServiceImpl(UkerServerProperties ukerServerProperties, FileInnerService fileInnerService) {
        this.fileBasePath = ukerServerProperties.getFileBasePath();
        this.fileInnerService = fileInnerService;
    }

    /**
     * 检查并初始化文件目录
     */
    @Override
    public void init() {
        File baseDir = new File(fileBasePath);
        if (!baseDir.exists()) {
            if (!baseDir.mkdir()) {
                throw new RuntimeException("failed creating fileBase dir");
            }
        }
        for (Filepath filepath : Filepath.values()) {
            File dir = new File(fileBasePath + filepath.getPath());
            if (!dir.exists()) {
                if (!dir.mkdir()) {
                    throw new RuntimeException("failed creating filepath dir ");
                }
            }
            fileInnerService.createSecondaryFolderOfFilepath(filepath, false);
        }
    }
}
